package com.chase.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chase.springboot.mapper.CommentMapper;
import com.chase.springboot.model.dto.CommentDTO;
import com.chase.springboot.model.entity.Comment;
import com.chase.springboot.model.entity.User;
import com.chase.springboot.model.vo.CommentVO;
import com.chase.springboot.service.ICommentService;
import com.qiniu.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.*;


@Slf4j
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public List<CommentVO> getCommentsByTopicID(String topicId) {
        List<CommentVO> lstBmsComment = new ArrayList<CommentVO>();
        try {
            lstBmsComment = commentMapper.getCommentsByTopicID(topicId);
        } catch (Exception e) {
            log.info("lstBmsComment失败");
            System.out.println(e.getMessage());
        }
        return lstBmsComment;
    }

    @Override
    public Comment create(CommentDTO dto, User user) {
        Comment comment = Comment.builder()
                .userId(user.getId())
                .content(dto.getContent())
                .topicId(dto.getTopicId())
                .createTime(new Date())
                .replyId(dto.getReplyId())
                .build();
        this.baseMapper.insert(comment);
        return comment;
    }

    @Override
    public List<CommentVO> getFirstLayerComments(String topicId) {
        List<CommentVO> comments = commentMapper.getFirstLayerComments(topicId);
        for (CommentVO comment : comments){
            //获取子评论列表
            List<CommentVO> childs = getCommentsByCommentId(comment.getId());
            if (childs != null && childs.size() > 0){
                //设置子评论
                comment.setChilds(childs);
            }
        }
        return comments;
    }



    private List<CommentVO> commentsList;
    @Override
    public List<CommentVO> getCommentsByCommentId(String commentId) {
        commentsList = new ArrayList<>();
        getCommentsByDfs(commentId);
        return commentsList;
    }

    @Override
    public boolean deleteCommentAndChilds(String commentId) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("id", commentId);
        wrapper.or().eq("reply_id", commentId);
        commentMapper.delete(wrapper);
        return true;
    }

    /**
     * 递归获取子评论
     * @param commentId
     */
    private void getCommentsByDfs(String commentId) {
        List<CommentVO> comments = commentMapper.getCommentsByCommentId(commentId);
        String replyUsername = commentMapper.getReplyUsername(commentId);
        if (comments != null && comments.size() > 0){
            for (CommentVO commentVo : comments){
                //添加回复用户名
                if (!StringUtils.isNullOrEmpty(replyUsername))
                    commentVo.setReplyUsername(replyUsername);
                //添加进列表
                commentsList.add(commentVo);
                //递归
                getCommentsByDfs(commentVo.getId());
            }
        }
    }

}
